<?php
/*
 * Created on Feb 5, 2009
 * @author: ANH
 * @PARAM rtype is rootType: example: caves is type then attraction is rtype
 * @filesource: modules/place/controllers/PlaceController.php
 */

class Place_DatabaseController extends Clu_Controller_Action_Hvn
{
	protected $mode;
	public function init()
	{
		//require_once  (APP_PATH.'/modules/article/Model/Article.php');
		//require_once  (APP_PATH.'/modules/category/Model/Category.php');
		//require_once  (APP_PATH.'/modules/comment/Model/Comment.php');
		//require_once  (APP_PATH.'/modules/user/Model/User.php');
		$this->m = new place_Model_Place();
		$this->mCity = new place_Model_City();
		$this->mDistrict = new place_Model_District();
		$this->mVillage = new place_Model_Village();
		$this->mArticle = new article_Model_Service();
		$this->mCategory = new category_Model_Category();
		$this->mCategory->setTables('place');
		$this->mVicinity = new place_Model_Vicinity();
		parent::init();
	}
    
    public function updatedAction()
    {
	    $db = $this->db;
	    $sql = "SELECT * FROM places";
	    $results = $this->db->fetchAll($sql);
	    foreach($results as $k => $v)
	    {
	    	try
	    	{
		    	$this->m->saveRaw(null, $v, $v['id'], false);
	    	}
	    	catch(Exception $e)
	    	{
	    		$this->view->errorMessage .= $e->getMessage() . "<br/>" . t(723);
		    	$e->getMessage();
	    	}
	    }
	    die('finish');
    }
    
    /*
     * update category when 'type' != NULL
     * 
     */
    public function updateTypeCategoryAction()
    {
	    $sql = "SELECT p.id, p.rtype, p.type FROM places p WHERE p.checked=0";
	    $results = $this->db->fetchAll($sql);
	    $data = array(7 => array('rtype' => 3, 'type' => 28),
	    			  8 => array('rtype' => 3, 'type' => 29),
	    			  9 => array('rtype' => 3, 'type' => 30),
	    			  10 => array('rtype' => 3, 'type' => 31),
	    			  11 => array('rtype' => 3, 'type' => 32), 
	    			  12 => array('rtype' => 3, 'type' => 33),
	    			  13 => array('rtype' => 3, 'type' => 28),
	    			  16 => array('rtype' => 3, 'type' => 27),
	    			  45 => array('rtype' => 2, 'type' => 'NULL'),
	    			  59 => array('rtype' => 2, 'type' => 21),
	    			  60 => array('rtype' => 2, 'type' => 21),
	    			  70 => array('rtype' => 2, 'type' => 26),
	    			  71 => array('rtype' => 2, 'type' => 26),
	    			  47 => array('rtype' => 4, 'type' => 'NULL'),
	    			  48 => array('rtype' => 4, 'type' => 'NULL'),
	    			  76 => array('rtype' => 5, 'type' => 54),
	    			  49 => array('rtype' => 5, 'type' => 53),
	    			  50 => array('rtype' => 5, 'type' => 54),
	    			  51 => array('rtype' => 5, 'type' => 52),
	    			  53 => array('rtype' => 6, 'type' => 35),
	    			  54 => array('rtype' => 6, 'type' => 36),
	    			  55 => array('rtype' => 6, 'type' => 36),
	    			  78 => array('rtype' => 8, 'type' => 'NULL'),
	    			  79 => array('rtype' => 8, 'type' => 'NULL'),
	    			  80 => array('rtype' => 8, 'type' => 'NULL'), 
	    			  28 => array('rtype' => 11, 'type' => 'NULL'),
	    			  84 => array('rtype' => 12, 'type' => 'NULL'),
	    			  85 => array('rtype' => 12, 'type' => 'NULL'),
	    			  86 => array('rtype' => 12, 'type' => 'NULL'),
	    			  87 => array('rtype' => 12, 'type' => 'NULL'),
	    			  52 => array('rtype' => 13, 'type' => 41),
	    			  32 => array('rtype' => 14, 'type' => 'NULL'),
					  93 => array('rtype' => 14, 'type' => 'NULL'),
	    			  77 => array('rtype' => 15, 'type' => 'NULL'), 
	    			  37 => array('rtype' => 16, 'type' => 'NULL'),
	    			  38 => array('rtype' => 17, 'type' => 'NULL'),
	    			  40 => array('rtype' => 18, 'type' => 'NULL'),
	    			  39 => array('rtype' => 18, 'type' => 'NULL'),
	    			  41 => array('rtype' => 18, 'type' => 'NULL'),
	    			  56 => array('rtype' => 18, 'type' => 'NULL'),
	    			  73 => array('rtype' => 18, 'type' => 'NULL'),
	    			  30 => array('rtype' => 19, 'type' => 37),
	    			  31 => array('rtype' => 19, 'type' => 38),
	    			  33 => array('rtype' => 19, 'type' => 'NULL'),
	    			  34 => array('rtype' => 19, 'type' => 'NULL'),
	    			  35 => array('rtype' => 19, 'type' => 'NULL'),
	    			  36 => array('rtype' => 19, 'type' => 'NULL'),
	    			  69 => array('rtype' => 19, 'type' => 'NULL'),
	    			  27 => array('rtype' => 20, 'type' => 'NULL'),
	    			  23 => array('rtype' => 8, 'type' => 'NULL'),
	    		);

	    foreach($data as $key => $val)
	    {
		    foreach($results as $k => $v)
		    {
		    	if($a = explode(',', $v['type']))
		    	{
		    		foreach($a as $aVal)
		    		{
			    		if($key == trim($aVal))
			    		{
			    			$sql = "UPDATE places SET places.rtype=" . $val['rtype'] . ", places.type=" . $val['type'] . ", places.checked=1 WHERE id=" . $v['id'];
					    	try{
						    	$this->db->query($sql);
						    }
						    catch(Exception $e)
						    {
						    	die_a($e);
						    	$e->getMessage();	
						    }
		    			}
		    		}
			    }
		    }
	    }
	    die('finish');
    }
    
    /*
     * update category when 'type' === NULL
     */
    public function updateRtypeCategoryAction()
    {
	    $sql = "SELECT p.id, p.rtype, p.type FROM places p WHERE p.checked=0";
	    $results = $this->db->fetchAll($sql);
	    $rtypeData = array(
	    				0 => array('rtype' => 15, 'type' => 'NULL'),
	    				7 => array('rtype' => 3, 'type' => 28),
	    				8 => array('rtype' => 3, 'type' => 29),
	    				9 => array('rtype' => 3, 'type' => 30),
	    				10 => array('rtype' => 3, 'type' => 31),
	    				11 => array('rtype' => 3, 'type' => 32),
	    				12 => array('rtype' => 3, 'type' => 33),
	    				13 => array('rtype' => 3, 'type' => 28),
	    				15 => array('rtype' => 3, 'type' => 27),
	    				16 => array('rtype' => 3, 'type' => 27),
	    				17 => array('rtype' => 19, 'type' => 'NULL'),
	    				18 => array('rtype' => 18, 'type' => 'NULL'),
	    				20 => array('rtype' => 4, 'type' => 'NULL'),
	    				23 => array('rtype' => 8, 'type' => 'NULL'),
	    				24 => array('rtype' => 12, 'type' => 'NULL'),
	    				27 => array('rtype' => 20, 'type' => 'NULL'),
	    				88 => array('rtype' => 55, 'type' => 'NULL'),
	    				90 => array('rtype' => 56, 'type' => 57),
	    				91 => array('rtype' => 56, 'type' => 58),
	    			);
	    foreach($rtypeData as $key => $val)
	    {
		    foreach($results as $k => $v)
		    {
	    		if($key == $v['rtype'])
	    		{
	    			$sql = "UPDATE places SET places.rtype=" . $val['rtype'] . ", places.type=" . $val['type'] . ", places.checked=1 WHERE id=" . $v['id'];
			    	try{
				    	$this->db->query($sql);
				    }
				    catch(Exception $e)
				    {
				    	die_a($e);
				    	$e->getMessage();	
				    }
    			}
		    }
	    }
	    die('finish');
    }
    
    public function  updatePosterNameAction()
    {
    	$i = 0;
	    //require_once(APP_PATH . "/modules/user/Model/User.php");
	    $uModel = new user_Model_User();

    	for ($i = 0; $i < 20000; $i++)
    	{
    		$p = $this->m->findByPrimary($i);
    		if (!$p)
    			continue;
//    		die_a(get_class($p));
	    	$metadata = unserialize($p['metadata']);
	    	$posterId = $p['poster_id'];
	    	$u = $uModel->findByPrimary($posterId);
	    	$metadata['poster_name'] = $u['name'];
//	    	die_a($metadata);
	    	try
	    	{
	    		$p['metadata'] = serialize($metadata);
	    		$p->save();
	    		$p['checked'] = 1;
	    		$p->save();
	    	}
	    	catch(Exception $e)
	    	{
	    		$this->view->errorMessage .= $e->getMessage() . "<br/>" . t(723);
		    	die($e->getMessage());
	    	}
    	}
    	//end for. Finished looping through all places
	    die('finish');
    		  
    }
}
?>
